<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\MaskedInputAsset;
use app\assets\TableAsset;
use app\models\Card;
use app\models\CardAttr;
use app\models\CardAttrValue;
use app\models\CardBasic;
use app\models\KeyMap;
use app\models\Package;
use app\models\UserPackage;
use app\widgets\LinkPager;
use app\widgets\ManagerTableOp;
use app\widgets\SortColumnLink;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $billList app\models\CardBill[]
 * @var $attrList app\models\CardAttr[] 需要在列表中显示的属性列表
 * @var $pagination yii\data\Pagination
 */

ApiAsset::register($this);
LayerAsset::register($this);
MaskedInputAsset::register($this);
TableAsset::register($this);

$this->title = '账单列表';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
<div class="form-group">
    <label for="search_month" class="sr-only">Month</label>
    <?php echo Html::textInput('search_month', Yii::$app->request->get('search_month'), ['id'=>'search_month', 'class'=>'form-control masked', 'placeholder'=>'月份', 'data-mask'=>'9999-99', 'style'=>'max-width:80px;']);?>
</div>
<div class="form-group">
    <label for="search_package_id" class="sr-only">套餐</label>
    <?php echo Html::dropDownList('search_package_id', Yii::$app->request->get('search_package_id'), ArrayHelper::map(UserPackage::find()->andWhere(['uid' => $uid])->all(), 'package_id', 'package.name'), ['id' => 'search_package_id', 'class' => 'form-control', 'prompt' => '搜索套餐']);?>
</div>
<div class="form-group">
    <label for="search_iccid" class="sr-only">ICCID</label>
    <?php echo Html::textInput('search_iccid', Yii::$app->request->get('search_iccid'), ['id'=>'search_iccid', 'class'=>'form-control', 'placeholder'=>'ICCID', 'style' => 'min-width:180px;']);?>
</div>
<div class="form-group">
    <button class="btn btn-primary btn-sm">搜索</button>
    <?php if (!empty(Yii::$app->request->get('search_month')) && !empty(Yii::$app->request->get('search_username'))) {?>
        <a href="<?php echo Url::current(['export'=>'excel']);?>">导出Excel</a>
    <?php }?>
</div>
<?php echo Html::endForm();?>
<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl">编号</span>
            </label>
        </th>
        <th>月份</th>
        <th>套餐</th>
        <th>ICCID/MSISDN/IMSI</th>
        <th>流量(MB)</th>
        <th>短信</th>
        <th>语音</th>
        <th>金额</th>
        <?php foreach ($attrList as $attr) {?>
            <th><?php echo Html::encode($attr->name);?></th>
        <?php }?>
        <?php if (!empty($attrList)) {?>
            <th><?php echo SortColumnLink::widget(['txt' => '属性更新时间', 'name' => 'card.attr_update_time']);?></th>
        <?php }?>
        <th>操作</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($billList as $bill) {?>
        <tr>
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $bill->id;?>" /><span class="lbl"><?php echo $bill->id;?></span></label></td>
            <td><?php echo $bill->month;?></td>
            <td><?php echo Html::encode($bill->package->name);?></td>
            <td><a href="<?php echo Url::to(['/user/card/view', 'id' => $bill->card_id]);?>">
                    <?php echo empty($bill->card->iccid) ? '----' : Html::encode($bill->card->iccid);
                    echo '<br />';
                    echo empty($bill->card->msisdn) ? '----' : Html::encode($bill->card->msisdn);
                    echo '<br />';
                    echo empty($bill->card->imsi) ? '----' : Html::encode($bill->card->imsi);?>
                </a>
            </td>
            <td><?php echo $bill->data/1024/1024;?></td>
            <td><?php echo $bill->sms;?></td>
            <td><?php echo $bill->voice;?></td>
            <td><?php echo $userPackage->cost_price;?></td>
            <?php foreach ($attrList as $attr) {?>
                <td style="white-space: pre;"><?php $attrValue = CardAttrValue::findOne(['uid' => $bill->card->uid, 'attr_id' => $attr->id, 'card_id' => $bill->card->id]);
                    if (!empty($attrValue)) {
                        echo Html::encode($attr->getHtml($attrValue->value));
                    }?></td>
            <?php }?>
            <?php if (!empty($attrList)) {?>
                <td><?php echo Yii::$app->formatter->asDatetime($bill->card->attr_update_time);?></td>
            <?php }?>
            <td><?php echo ManagerTableOp::widget(['items' => [
                ]]);?>
            </td>
        </tr>
    <?php }?>
    </tbody>
</table>
<?php echo LinkPager::widget(['pagination' => $pagination]);?>

